package datastructures;

import java.util.Hashtable;
import java.util.Vector;


public class MapfileWrapper {
	
	private SNPInfo[] snps;
	private Hashtable<String, Integer> snpIndex;
	
	public MapfileWrapper(SNPInfo[] mapFile) {
		snps = mapFile;
		snpIndex = new Hashtable<String, Integer>();
		for(int index=0; index<mapFile.length; index++)
			snpIndex.put(mapFile[index].getRsid(), index);
	}
	
	public int getNumberOfSNPs() {
		return snps.length;
	}
	
	public SNPInfo getSNPInfo (int index) {
		if(index < 0 || index >= snps.length)
			return null;
		return snps[index];
	}
	
	public int getSNPIndex(String rsID) {
		return snpIndex.get(rsID);
	}
	
	
	public int[] interactionToVariables(String[] interaction) {
		int[] variables = new int[interaction.length/2];
		//System.out.println(interaction[0]+","+interaction[1]+","+interaction[2]+","+interaction[3]);
		for(int i=0; i<interaction.length/2; i++) {
			String mode = interaction[2*i];
			int modeOffset = ( mode.compareTo("d") == 0 ) ? 0 : 1; 
			String rsID = interaction[2*i+1].replace(" ","");
			int snpOffset = 2*snpIndex.get( rsID );
			variables[i] = snpOffset + modeOffset;
		}
		return variables;
	}
	
	
	
	public Vector<SNPInfo> getSNPs (int[] variables) {
		
		boolean[] uniqueVariables = new boolean[snps.length];
		int reducedNumberOfSNPs = 0;
		for(int i=0; i<variables.length; i++) {
			int snpNo = (int) Math.floor (variables[i] / 2);
			if ( !uniqueVariables[snpNo] ) {
				uniqueVariables[snpNo] = true;
				reducedNumberOfSNPs++;
			}
		}
		
		Vector<SNPInfo> reducedSNPset = new Vector<SNPInfo>();
		for(int snp=0; snp<uniqueVariables.length; snp++) {
			if ( uniqueVariables[snp] )
				reducedSNPset.add( snps[snp] );
		}
		return reducedSNPset;
		
	}
	
}
